/*
 * Copyright 2018 xwc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.github.xwc.view;

import com.github.xwc.view.util.MyUtils;
import com.github.xwc.view.util.PixelMapFactoryUtil;
import ohos.agp.components.AttrHelper;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.render.Path;
import ohos.agp.render.PathEffect;
import ohos.agp.render.PixelMapHolder;
import ohos.agp.utils.Color;
import ohos.agp.utils.Rect;
import ohos.agp.utils.RectFloat;
import ohos.agp.utils.TextAlignment;
import ohos.app.Context;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;

/**
 * Created by xwc on 2018/2/24.
 */

public class ShapeView extends Shape {

    /**
     * 圆形
     */
    public static final int CIRCLE = 0;

    /**
     * 三角形
     */
    public static final int TRIANGLE = 2;
    /**
     * 心形
     */
    public static final int HEART = 3;
    /**
     * 正多边形
     */
    public static final int POLYGON = 4;
    /**
     * 五角星形
     */
    public static final int STAR = 5;
    /**
     * 对角线形
     */
    public static final int DIAGONAL = 6;

    //Default is CIRCLE
    protected int shapeType;

    private float percentBottom = 0.5f;
    private float percentLeft = 0f;
    private float percentRight = 0f;

    private float heartRadian = 0.2f; // radians of heart
    private float heartYPercent = 0.16f;

    private int sides = 4;
    private float turn = 0f; // 旋转角度

    //diagonal
    public static final int POSITION_BOTTOM = 1;
    public static final int POSITION_TOP = 2;
    public static final int POSITION_LEFT = 3;
    public static final int POSITION_RIGHT = 4;
    public static final int DIRECTION_LEFT = 1;

    private int diagonalPosition = POSITION_TOP;
    private int diagonalDirection = DIRECTION_LEFT;
    private int diagonalAngle = 0;

    private IClipPath clipPath;

    public ShapeView(Context context) {
        super(context);
    }

    public ShapeView(Context context,AttrSet attrs) {
        this(context, attrs, "0");
    }

    public ShapeView(Context context, AttrSet attrs, String defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(attrs);
    }

    private void init(AttrSet attrs) {
        if (attrs != null) {

            //获取图形形状
            shapeType = getShapeViewShapeType(AttrUtil.getStringValue(attrs, "shape_type", "circle"));

            if (shapeType == TRIANGLE) {
                percentLeft = AttrUtil.getFloatValue(attrs, "shape_triangle_percentLeft", percentLeft);
                percentBottom = AttrUtil.getFloatValue(attrs, "shape_triangle_percentBottom", percentBottom);
                percentRight = AttrUtil.getFloatValue(attrs, "shape_triangle_percentRight", percentRight);
            }

            if (shapeType == HEART) {
                heartRadian = AttrUtil.getFloatValue(attrs, "shape_heart_radian", heartRadian);
                heartYPercent = AttrUtil.getFloatValue(attrs, "shape_heart_YPercent", heartYPercent);
            }

            if (shapeType == POLYGON) {
                sides = AttrUtil.getIntegerValue(attrs, "shape_polygon_side", sides);
                turn = AttrUtil.getFloatValue(attrs, "shape_polygon_turn", turn);
            }

            if (shapeType == DIAGONAL) {
                diagonalAngle = AttrUtil.getIntegerValue(attrs, "shape_diagonal_angle", diagonalAngle);
                diagonalDirection =getShapeDiagonalDirection(AttrUtil.getStringValue(attrs, "shape_diagonal_direction", "left"))  ;
                diagonalPosition = getShapeDiagonalPosition(AttrUtil.getStringValue(attrs, "shape_diagonal_position", "top")) ;
            }

        }

        super.setClipHelper(new ClipHelper() {
            @Override
            public Path createClipPath(int width, int height) {
                final Path path = new Path();
                // 出去Icl不报错了
                clipPath = IClipPathFactory.create(shapeType, ShapeView.this);
                clipPath.setClipPath(path, width, height);
                return path;
            }
        });
    }

    private int getShapeDiagonalPosition(String position){
        int mPosition = diagonalPosition;
        switch (position){
            case "bottom":
                mPosition = 1;
                break;

            case "top":
                mPosition = 2;
                break;

            case "left":
                mPosition = 3;
                break;

            case "right":
                mPosition = 4;
                break;
        }
        return mPosition;
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);

        PixelMap pixelMap = PixelMapFactoryUtil.getPixelMapFromResource(getContext(), ResourceTable.Media_shapeimage, new ImageSource.DecodingOptions());
        canvas.drawPixelMapHolderRect(new PixelMapHolder(pixelMap), new RectFloat(0,0,getWidth(),getHeight()), borderPaint);

        Paint paint = new Paint();
        paint.setColor(Color.WHITE);
        paint.setTextAlign(TextAlignment.CENTER);
        paint.setTextSize(AttrHelper.fp2px(20,getContext()));
        canvas.drawText(paint, "ShapeView", (float) getWidth()/2, (float) getHeight()/2);
        if (borderWidthPx > 0) {
            borderPaint.setStrokeWidth(borderWidthPx);
            borderPaint.setColor(new Color( Color.getIntColor(borderColor)));
            if (borderDashGap > 0 && borderDashWidth > 0) {
                borderPaint.setPathEffect(new PathEffect(new float[]{borderDashWidth, borderDashGap}, 0));
            }
            switch (shapeType) {
                case CIRCLE:
                    canvas.drawCircle(getWidth() / 2f, getHeight() / 2f, Math.min((getWidth() - borderWidthPx) / 2f, (getHeight() - borderWidthPx) / 2f), borderPaint);
                    break;
                case HEART:

                    canvas.drawPath(getClipHelper().mPath, borderPaint);
                    break;
                case POLYGON:
                    canvas.drawPath(getClipHelper().mPath, borderPaint);
                    break;
                case STAR:
                    canvas.drawPath(getClipHelper().mPath, borderPaint);
                    break;
                case TRIANGLE:
                    canvas.drawPath(getClipHelper().mPath, borderPaint);
                break ;
                case DIAGONAL:
                    canvas.drawPath(getClipHelper().mPath, borderPaint);
                    break;
            }

        }
    }

    public float getPercentBottom() {
        return percentBottom;
    }

    public void setPercentBottom(float percentBottom) {
        this.percentBottom = percentBottom;
    }

    public float getPercentLeft() {
        return percentLeft;
    }

    public void setPercentLeft(float percentLeft) {
        this.percentLeft = percentLeft;
    }

    public float getPercentRight() {
        return percentRight;
    }

    public void setPercentRight(float percentRight) {
        this.percentRight = percentRight;
    }

    public float getHeartRadian() {
        return heartRadian;
    }

    public void setHeartRadian(float heartRadian) {
        this.heartRadian = heartRadian;
    }

    public float getHeartYPercent() {
        return heartYPercent;
    }

    public void setHeartYPercent(float heartYPercent) {
        this.heartYPercent = heartYPercent;
    }

    public int getSides() {
        return sides;
    }

    public void setSides(int sides) {
        this.sides = sides;
    }

    public float getTurn() {
        return turn;
    }

    public void setTurn(float turn) {
        this.turn = turn;
    }

    public int getDiagonalPosition() {
        return diagonalPosition;
    }

    public void setDiagonalPosition(int diagonalPosition) {
        this.diagonalPosition = diagonalPosition;
    }

    public int getDiagonalDirection() {
        return diagonalDirection;
    }

    public void setDiagonalDirection(int diagonalDirection) {
        this.diagonalDirection = diagonalDirection;
    }

    public int getDiagonalAngle() {
        return diagonalAngle;
    }

    public void setDiagonalAngle(int diagonalAngle) {
        this.diagonalAngle = diagonalAngle;
    }

    public int getShapeType() {
        return shapeType;
    }

    public void setShapeType(int shapeType) {
        this.shapeType = shapeType;
    }

    private int getShapeViewShapeType(String shapeType) {
        int type = 0;
        switch (shapeType) {
            case "circle":
                type = 0;
                break;
            case "roundRect":
                type = 1;
                break;
            case "triangle":
                type = 2;
                break;
            case "heart":
                type = 3;
                break;
            case "polygon":
                type = 4;
                break;
            case "star":
                type = 5;
                break;
            case "diagonal":
                type = 6;
                break;
        }
        return type;
    }

    private int getShapeDiagonalDirection(String direction){
        int mdirection=diagonalDirection;
        switch (direction){
            case "left":
                mdirection = 1;
                break;
            case "right":
                mdirection = 2;
                break;
        }
        return mdirection;
    }
}
